﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using DigiLib;
using System.IO;

public partial class controls_admin_VideoEdit : Admin_Controls_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            base.ModuleByContent(ddlModule, "VIDEO", "THUMB_IMAGE", 1);
            if (ddlModule.Items.Count == 0)
                ddlModule.Items.Add(new ListItem("Chưa có module video nào", "-1"));
            loadAdv();
        }
    }
    private void loadAdv()
    {
        try
        {
            ObjDT = BO_portal_videos.Search(Id);
            if (ObjDT != null && ObjDT.Rows.Count > 0)
            {
                if (!ObjDT.Rows[0]["PATH"].ToString().Equals(string.Empty))
                    if (ObjDT.Rows[0]["PATH"].ToString().StartsWith("http://"))
                    {
                        txtYoutube.Text = ObjDT.Rows[0]["PATH"].ToString();
                        try
                        {
                            string video_id = ObjDT.Rows[0]["PATH"].ToString().Substring(ObjDT.Rows[0]["PATH"].ToString().IndexOf("watch?v="), 19).Replace("watch?v=", "");
                            imgAdv.InnerHtml = "<object width=\"216\" height=\"161\">"
                            + "<param name=\"movie\" value=\"http://www.youtube.com/v/" + video_id + "?fs=1&amp;hl=en_US&amp;rel=0\">"
                            + "</param>"
                            + "<param name=\"allowFullScreen\" value=\"true\"></param>"
                            + "<param name=\"allowscriptaccess\" value=\"always\"></param>"
                            + "<embed src=\"http://www.youtube.com/v/" + video_id + "?fs=1&amp;hl=en_US&amp;rel=0\""
                            + "type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\""
                            + "width=\"216\" height=\"161\"></embed></object>";
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                    else
                        imgAdv.InnerHtml = "<object height=' 100' border='0' width='200' codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"><param value=\"/upload/" + ObjDT.Rows[0]["PATH"].ToString() + "\" name=\"movie\"><param value=\"always\" name=\"AllowScriptAccess\"><param value=\"High\" name=\"quality\"><param value=\"transparent\" name=\"wmode\"><param value=\"#\" name=\"FlashVars\"><embed  height=' 100' border='0' width='300' allowscriptaccess=\"always\" wmode=\"transparent\" loop=\"true\" play=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" flashvars=\"#\" src=\"/upload/" + ObjDT.Rows[0]["PATH"].ToString() + "\"></object>";

                //imgAdv.InnerHtml = "<object height=' 100' border='0' width='200' codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"><param value=\"/upload/" + ObjDT.Rows[0]["PATH"].ToString() + "\" name=\"movie\"><param value=\"always\" name=\"AllowScriptAccess\"><param value=\"High\" name=\"quality\"><param value=\"transparent\" name=\"wmode\"><param value=\"#\" name=\"FlashVars\"><embed  height=' 100' border='0' width='300' allowscriptaccess=\"always\" wmode=\"transparent\" loop=\"true\" play=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" flashvars=\"#\" src=\"/upload/" + ObjDT.Rows[0]["PATH"].ToString() + "\"></object>";
                txtVideo.Text = ObjDT.Rows[0]["TITLE"].ToString();
                ctImages.Src = "/upload/" + ObjDT.Rows[0]["IMAGES"].ToString();
                ObjDT = BO_cms.Join_Tables_Select(Id, "FK_portal_videos_portal_modules", false);
                txtSortDisplay.Text = ObjDT.Rows[0]["VAR1"].ToString();
                ddlModule.SelectedValue = ObjDT.Rows[0]["ID_2"].ToString();
            }
        }
        catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi trong quá trình xử lý<br />" + ex.Message); }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ObjDT = BO_portal_videos.Search(Id);
        string fileName = base.Upload("video", String.Empty, fulVideo, divMessage, false);
        string images = base.Upload("video", String.Empty, fulImages, divMessage, false);
        if (string.IsNullOrEmpty(fileName))
        {
            fileName = txtYoutube.Text;
        }
        try
        {
            string title = txtVideo.Text.Trim();
            if (string.IsNullOrEmpty(fileName))
                fileName = ObjDT.Rows[0]["PATH"].ToString();
            if (string.IsNullOrEmpty(images))
                images = ObjDT.Rows[0]["IMAGES"].ToString();
            string result = BO_portal_videos.Update(Id, title, fileName, images, false, Convert.ToDateTime(ObjDT.Rows[0]["CREATED_DATE"]), ObjDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, Administrator.USER_NAME, true);
            if (result.Equals("0"))
                divMessage.InnerHtml = MessageBox.Error("Thông Báo", "Lỗi trong quá trình truyền tải dữ liệu. Mong bạn trở lại sau .");
            else
            {
                DigiLib.SqlUtils.ExecuteNonQuery("Update [dbo].[portal_join_tables] Set [ID_2] = " + ddlModule.SelectedValue + ", [VAR1] = " + txtSortDisplay.Text + " WHERE ID_1 = " + Id.ToString() + " AND [FOREIGN_KEY] = 'FK_portal_videos_portal_modules'");
                divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Thực hiện thành công .");
                Response.Redirect("Default.aspx?control=Video");
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi xảy ra !<br />" + ex.Message);
        }
    }
}
